﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class home_Diagram : System.Web.UI.Page
{
    public string projectname = "";
    int projectId = 0;
    int userId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id_user"] == null || Session["permission"] == null || ((int)Session["permission"]) <= 1)
            Response.Redirect("../login.aspx");
        
        #region get project and user
        // get current project------------------------------------
        try
        {
            projectId = Int32.Parse(Request.QueryString["project"]);
        }
        catch
        {
            projectId = 0;
        }
        // get current user--------------------------------------
        try
        {
            userId = Int32.Parse(Request.QueryString["user"]);
        }
        catch
        {
            userId = 0;
        }
        #endregion

        if (!IsPostBack)
        {
            BindData();
            try
            {
                listproject.Items.FindByValue("0").Selected = false;
                listproject.Items.FindByValue(projectId.ToString()).Selected = true;
            }
            catch
            {
                listproject.Items.FindByValue("0").Selected = true;
            }
        }
    }

    private void BindData()
    {
        listproject.Items.Clear();
        ListItem aItem = new ListItem("-- Chọn dự án --", "0");
        listproject.Items.Add(aItem);

        // get list names of projects
        int id = Int32.Parse(Session["id_user"].ToString());
        SqlParameter param = new SqlParameter("@id_user", id);
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        DataTable dtTable = dal.SelectTableSP("proUser_ProjectName_SEL", param);
        
        // display names of projects in dropdownlist
        foreach (DataRow row in dtTable.Rows)
        {
            ListItem item = new ListItem(row["name"].ToString(), row["id"].ToString());
            listproject.Items.Add(item);
        }
    }

    protected void listproject_SelectedIndexChanged(object sender, EventArgs e)
    {
        string project = ((DropDownList)sender).SelectedValue;
        if (project != "0")
        {
            string sql = "SELECT name FROM project WHERE id=" + comm.to_sql(project, "number");
            DataTable dtTable = dal.SelectTable(sql);
            if (dtTable.Rows.Count == 1)
            {
                projectname = dtTable.Rows[0]["name"].ToString();
                imageProject.ImageUrl = "../home/drawproject.aspx?project=" + project;
            }
        }
    }
}
